﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
//using Classes.Magento;
using Classes.MgAPI;

namespace Classes
{
    public class MagApi
    {
        LogWriter writer = LogWriter.Instance;
        //private string sessionID = "";
        private Mage_Api_Model_Server_V2_HandlerPortTypeClient ms = new Mage_Api_Model_Server_V2_HandlerPortTypeClient();
               
       // writer.WriteToLog(message);
        public string Login(string apiUser, string apiKey)
        {
            string o;
            try
            {
                 o=ms.login(apiUser, apiKey);
            }
            catch (System.ServiceModel.FaultException e)
            {
                writer.WriteToLog("erro: "+e.Message);
                o = "";
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = "";
            }
            return o;
        
        }

        public bool endSession(string sessionID)
        {
            return ms.endSession(sessionID);
        }


        #region Category
        public catalogCategoryTree getCategoryTree(string sessionID)
        {
            catalogCategoryTree o;
            try
            {
                o = ms.catalogCategoryTree(sessionID, "1", "");
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new catalogCategoryTree();
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogCategoryTree();
            }
            return o;

        }

        public List<catalogCategoryEntity> getCategoryList(string sessionID)
        {
            List<catalogCategoryEntity> o = new List<catalogCategoryEntity>(); ;
            try
            {
                catalogCategoryTree CategoryTree = getCategoryTree(sessionID);
                CategoryRecursive(CategoryTree.children, o);

            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new List<catalogCategoryEntity>();
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

            //List<catalogCategoryEntity> CategoryList = new List<catalogCategoryEntity>();
            //CategoryRecursive(CategoryTree.children, CategoryList);
            //return CategoryList;
        }

        private void CategoryRecursive(catalogCategoryEntity[] CategoryTree, List<catalogCategoryEntity> CategoryList)
        {
            foreach (catalogCategoryEntity Category in CategoryTree)
            {
                CategoryList.Add(Category);
                CategoryRecursive(Category.children, CategoryList);
            }
        }

        public catalogCategoryTree getProductAttributeList(string sessionID, string parentId, string storeView)
        {
            catalogCategoryTree o;
            try
            {
                o = ms.catalogCategoryTree(sessionID, parentId, storeView);//ms.catalogProductAttributeSetList(sessionID);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new catalogCategoryTree();
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogCategoryTree();
            }
            return o;

        }


        public bool ProductCategoryUpdate(string sessionID, int categoryID, string productID, string position, string productIdentifier)
        {
            bool o = false;
            try
            {
                o = ms.catalogCategoryUpdateProduct(sessionID, categoryID, productID, position, productIdentifier);
            }
            catch (System.ServiceModel.FaultException e)
            {
                
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

        }
        public bool CategoryRemoveProduct(string sessionID, int categoryID, string productID, string position, string productIdentifier)
        {
            bool o = false;
            try
            {
                o = ms.catalogCategoryRemoveProduct(sessionID, categoryID, productID, productIdentifier);
            }
            catch (System.ServiceModel.FaultException e)
            {
                
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

        }

        public bool CategoryAssignProduct(string sessionID, int categoryID, string productID, string position, string productIdentifier)
        {
            bool o = false;
            try
            {
                o = ms.catalogCategoryAssignProduct(sessionID, categoryID, productID, position, productIdentifier);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = false;
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

        }

        #endregion

        #region Produtos
        public catalogProductAttributeSetEntity[] getProductAttribute(string sessionID)
        {
            catalogProductAttributeSetEntity[] o;
            try
            {
                o = ms.catalogProductAttributeSetList(sessionID);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new catalogProductAttributeSetEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogProductAttributeSetEntity[1];

            }
            return o;
        }
        

        public catalogProductTypeEntity[] getProductType(string sessionID)
        {

            catalogProductTypeEntity[] o;
            try
            {
                o=ms.catalogProductTypeList(sessionID);
            }
            catch (System.ServiceModel.FaultException e)
            {
            o = new catalogProductTypeEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogProductTypeEntity[1];
            }
            return o;

        }

        public bool ProductAttributeUpdate(string sessionID, string attribute, catalogProductAttributeEntityToUpdate AttributeData)
        {
            bool o = false;
            try
            {
                o=ms.catalogProductAttributeUpdate(sessionID, attribute, AttributeData);
            }
            catch (System.ServiceModel.FaultException e)
            {
                
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;
             
        }


        //public catalogProduct getProductList(string sessionID, filters filtro)
        ////{
        //   return ms.catalogProductList( sessionID, filters, "");
        ////       // ms.customerCustomerList()
        //}

        //public catalogProductEntity[] getProductList(string sessionID, filters filters, string storeview) //
        //{
        //    Magento ws = new Magento();
        //    return ms.catalogProductList(sessionID, filters, storeview);
        //}
        public catalogProductReturnEntity getProductInfo(string sessionID, string product, catalogProductRequestAttributes attributes, string identifierType)
        {
            catalogProductReturnEntity o;
            try
            {
                // Object o = new Object();
                o = ms.catalogProductInfo(sessionID, product, null, attributes, identifierType);
            }
            catch (System.ServiceModel.FaultException e)
            {

                o = new catalogProductReturnEntity();
                //o.description = e.Message;
                //o.product_id = "";

            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogProductReturnEntity();
            }
            return o;
        }


        public catalogProductImageEntity getProductMedia(string sessionID, string product, string file, string identifierType)
        {

            catalogProductImageEntity o;
            try
            {
                o = ms.catalogProductAttributeMediaInfo(sessionID, product, file, "", identifierType);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new catalogProductImageEntity();
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogProductImageEntity();
            }
            return o;
        }


        public catalogProductImageEntity[] getProductAttributeMediaList(string sessionID, string product, string identifierType)
        {

            catalogProductImageEntity[] o;
            try
            {
                o = ms.catalogProductAttributeMediaList(sessionID, product, null, identifierType);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new catalogProductImageEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new catalogProductImageEntity[1];
            }
            return o;
        }


        public string ProductMediaCreate(string sessionID, string product, catalogProductAttributeMediaCreateEntity data, string tipo)
        {

            string   o = "";
            try
            {
                o = ms.catalogProductAttributeMediaCreate(sessionID, product, data, "", tipo);
            }
            catch (System.ServiceModel.FaultException e)
            {
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;


        }

        //public  catalogProductAttributeEntity getProductAttribute(string sessionID, string product, string filename, string identifierType)
        //{
        //    return ms.(sessionID, product, filename, "", identifierType);
        //}

        public int newProduct(string sessionID, string type, string sku, string set, catalogProductCreateEntity productData, string storeView)
        {
            int o = 0;
            try
            {
                o = ms.catalogProductCreate(sessionID, type, set, sku, productData, storeView);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = -1;
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

        }

        public int UpdateStockItem(string sessionID, string productID, catalogInventoryStockItemUpdateEntity StockData)
        {
            int o = 0;
            try
            {
                o = ms.catalogInventoryStockItemUpdate(sessionID, productID, StockData);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = -1;
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);

            }
            return o;

        }
        #endregion
        #region Custumer
        public customerCustomerEntity[] getCustumerList(String sessionID, filters filters)
        {
            
                customerCustomerEntity[] o;
                try
                {
                    o=ms.customerCustomerList(sessionID, filters);
                }
                catch (System.ServiceModel.FaultException e)
                {
                o = new customerCustomerEntity[1];
                }
                return o;
   
              
        }

        public directoryCountryEntity[] getCoutryList(string sessionID)
        {
            directoryCountryEntity[] o;
            try
            {
                o=ms.directoryCountryList(sessionID);
            }
            catch (System.ServiceModel.FaultException e)
            {
            o = new directoryCountryEntity[1];
            }
            return o;
         
        }

        public customerAddressEntityItem[] getcustomerAddressList(string sessionID, int customerID)
        {
             customerAddressEntityItem[] o;
            try
            {
                o= ms.customerAddressList(sessionID, customerID);
            }
            catch (System.ServiceModel.FaultException e)
            {
            o = new customerAddressEntityItem[1];
            }
            return o;  
        }

        public int ClienteCreate(string sessionID, customerCustomerEntityToCreate customerData)
        {

            int o;
            try
            {
                o = ms.customerCustomerCreate(sessionID, customerData);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = 0;
            }
            return o;

        }

        public void CustomerEntityToCreate()
        {
            //  Classes.MgAPI.customerCustomerEntityToCreate x = new Classes.MgAPI.customerCustomerEntityToCreate();

        }
        public bool CustomerUpdate(string sessionID, int customerId, customerCustomerEntityToCreate customerData)
        {

            bool o;
            try
            {
                o = ms.customerCustomerUpdate(sessionID, customerId, customerData);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = false;
            }
            return o;
        }


        public customerCustomerEntity CustomerInfo(string sessionID, int customerId, string[] attributes)
        {

            customerCustomerEntity o;
            try
            {
                o = ms.customerCustomerInfo(sessionID, customerId, attributes);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new customerCustomerEntity();
            }
            return o;
        }

        public int CustomerAddressCreate(string sessionID, int customerId, customerAddressEntityCreate[] addressData)
        {
            int o = 0;
            try
            {
                foreach (customerAddressEntityCreate address in addressData)
                {
                    o = ms.customerAddressCreate(sessionID, customerId, address);
                }
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = -1;
            }
            return o;


        }

        


        #endregion



        public directoryRegionEntity[] getRegionList(string sessionID, string country)
        {
            directoryRegionEntity[] o;
            try
            {
                o = ms.directoryRegionList(sessionID, country);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new directoryRegionEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new directoryRegionEntity[1];
            }
            return o;

        }

        public storeEntity StoreInfo(string sessionID, string storeInfo)
        {
            storeEntity o;
            try
            {
                 o= ms.storeInfo(sessionID, storeInfo);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new storeEntity();
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new storeEntity();
            }
            return o; 
        }



 


        public storeEntity[] StoreList(string sessionID)
        {
            storeEntity[] o;
            try
            {
                o=ms.storeList(sessionID);
            }
            catch (System.ServiceModel.FaultException e)
            {
                o = new storeEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new storeEntity[1];
            }
            return o;             

        }



        public salesOrderListEntity[] getSalesOrderList(string sessionID, filters filtros)
        {
        salesOrderListEntity[] o;
        try
        {
            o=ms.salesOrderList(sessionID, filtros);
        }
        catch (System.ServiceModel.FaultException e)
        {
            o = new salesOrderListEntity[1];
        }
        catch (Exception e)
        {
            writer.WriteToLog("erro: " + e.Message);
            o= new salesOrderListEntity[1];
        }

        return o;
            
        }

        public salesOrderInvoiceEntity[] getSalesInvoiceList(string sessionID, filters filtros)
        {
            /**/
    salesOrderInvoiceEntity[]o;
    try
    {
    o=ms.salesOrderInvoiceList(sessionID, filtros);
    }
    catch (System.ServiceModel.FaultException e)
    {
    o = new salesOrderInvoiceEntity[1];
    }
    catch (Exception e)
    {
        writer.WriteToLog("erro: " + e.Message);
        o = new salesOrderInvoiceEntity[1];
    }
    return o;
    o = new salesOrderInvoiceEntity[1];
    
             
        }
        /*
o;
try
{
o=
}
catch (System.ServiceModel.FaultException e)
{
o = new ();
}
return o;
*/ 
        public salesOrderShipmentEntity[] getSalesShipmentList(string sessionID, filters filtros)
        {
            salesOrderShipmentEntity[] o;
            try
            {
            o=ms.salesOrderShipmentList(sessionID, filtros);
            }
            catch (System.ServiceModel.FaultException e)
            {
            o = new salesOrderShipmentEntity[1];
            }
            catch (Exception e)
            {
                writer.WriteToLog("erro: " + e.Message);
                o = new salesOrderShipmentEntity[1];
            }
            return o;   
        }


        public void getSalesOrderCreate(string sessionID, string invoiceIncrementId, orderItemIdQty itemsQty, string comment, string email, string includeComment)
        {
            //   ms.ementId, itemsQty, comment, email, includeComment);
        }

        public void getSalesInvoiceCreate(string sessionID, string invoiceIncrementId, orderItemIdQty itemsQty, string comment, string email, string includeComment)
        {
          //  ms.salesOrderInvoiceCreate(sessionID, invoiceIncrementId, itemsQty, comment, email, includeComment);
        }
        public void getSalesShipmentCreate(string sessionID, string invoiceIncrementId, orderItemIdQty itemsQty, string comment, string email, string includeComment)
        {
           // ms.salesOrderShipmentCreate(sessionID, invoiceIncrementId, itemsQty, comment, email, includeComment);
        }




    }
}
